package ServerDB;

import DataConnectionInterface.UserInfo;
import DataConnectionInterface.Card;
import DataConnectionInterface.CardInterface;

// Class with global data regarding the current game

public class GameState {

	private Card[] clientCards = new Card[2];
	private boolean[] playerReady = new boolean[2];
	
	// helps to decide who starts the game (random)
	private int[] randNumbers = new int[2]; 
	private boolean[] turns = new boolean[2];
	
	// number of cards after each round
	private int[] numOfCards = new int[2]; 
	private boolean[] isUpdated = new boolean[2]; 
	
	// user's information
	private UserInfo[] users = new UserInfo[2];
	
	// game status: win, lose or none
	private String[] status = new String[2];
	
	public GameState() {
		for (int i=0; i<clientCards.length; i++) {
			clientCards[i] = null;
			playerReady[i] = false;
		}
	}
	
	public void setUser(UserInfo user, int playerNumber){
		users[playerNumber] = user;
	}
	
	public UserInfo getUser(int playerNumber){
		return users[playerNumber];
	}
	
	public void setStatus(String stat, int playerNumber){
		status[playerNumber] = stat;
	}
	
	public String getStatus(int playerNumber){
		return status[playerNumber];
	}
	
	public boolean isUpdated(int playerNumber){
		return isUpdated[playerNumber];
	}
	
	public void setNumOfCards(int num, int playerNumber){
		numOfCards[playerNumber] = num;
		isUpdated[playerNumber] = true;
	}
	
	public int getNumOfCards(int playerNumber){
		isUpdated[playerNumber] = false;
		return numOfCards[playerNumber];
		
	}
	
	public void setRand(int rand, int playerNumber){
		randNumbers[playerNumber] = rand;
	}
	
	public boolean getBeginner(int playerNumber){
		boolean seed = randNumbers[0] + randNumbers[1] % 2 == 0 ? true : false;
		boolean playerIndex = (playerNumber == 0) ? false : true; 
		turns[playerNumber] = seed ^ playerIndex;
		return seed ^ playerIndex;
	}
	
	public boolean getTurns(int playerNumber){
		return turns[playerNumber];
	}

	public synchronized void setPlayerCard(Card card, int playerNumber) {
		this.clientCards[playerNumber] = card;
	}
	
	public synchronized Card getPlayerMove(int playerNumber) {
		Card ret = clientCards[playerNumber];
		clientCards[playerNumber] = null;
		return ret;
	}
	
	public synchronized void setPlayerReadiness(int playerNumber, boolean isReady) {
		this.playerReady[playerNumber] = isReady;
	}
	
	
	public synchronized boolean isPlayerReady(int playerNumber) {
		return this.playerReady[playerNumber];
	}


}
